package cn.com.xxlibrary.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import cn.com.xxlibrary.service.IImportService;
import cn.com.xxlibrary.util.ExcelTools;
import cn.com.xxlibrary.util.UtilTools;

import com.opensymphony.xwork2.ActionSupport;

public class ImportAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	IImportService importService;
	
	private String itype;
	
	// 文件控件属性变量--前台页面的input file 表单中的name属性
	private File fileUp;
	// 文件类型
	private String fileUpContentType;
	// 所选择的上传文件的名字
	private String fileUpFileName;


	@Action(value = "importExcel", results = {
			@Result(name = "book", location = "/listBook", type = "redirect"),
			@Result(name = "disk", location = "/listDisk", type = "redirect"),
			@Result(name = "peri", location = "/listPeri", type = "redirect"),
			@Result(name = "enter", location = "/common/importExcel.jsp") })
	public String importExcel() throws IOException {
		//if ("enter".equals(op)) {
		//	return "enter";
		//}
		if (!"application/vnd.ms-excel".equals(fileUpContentType)){
			UtilTools.sendMsg("文件格式不支持！");
			return "enter";
		}
		// 使用fileinputstream对象读取数据流
		FileInputStream fis = new FileInputStream(this.getFileUp());
		List<List> content = ExcelTools.readExcelContent(fis);
		// for (List list : content) {
		// System.out.println(list.toString());
		// }
		switch (itype) {
		case "book":
			importService.importBooks(content);
			break;

		case "disk":
			importService.importBooks(content);
			break;

		case "peri":
			importService.importPeriodicals(content);
			break;

		default:
			break;
		}
		fis = null;
		UtilTools.sendMsg("导入成功！");
		return itype;
	}

	public File getFileUp() {
		return fileUp;
	}

	public void setFileUp(File fileUp) {
		this.fileUp = fileUp;
	}

	public String getFileUpContentType() {
		return fileUpContentType;
	}

	public void setFileUpContentType(String fileUpContentType) {
		this.fileUpContentType = fileUpContentType;
	}

	public String getFileUpFileName() {
		return fileUpFileName;
	}

	public void setFileUpFileName(String fileUpFileName) {
		this.fileUpFileName = fileUpFileName;
	}

	public String getItype() {
		return itype;
	}

	public void setItype(String itype) {
		this.itype = itype;
	}
}
